package com.sdy.window;

import com.alibaba.fastjson.JSONObject;
import com.sdy.SerialFactory;
import com.sdy.po.SerialPO;
import netscape.javascript.JSObject;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

public class InfoWindow extends JDialog {

    private SerialFactory serialFactory;

    private List<JSONObject> resultList;

    private List<SerialPO> serialList;

    public InfoWindow(){

    }

    public InfoWindow(final SerialFactory serialFactory, List<JSONObject> resultList) {
        this.setModal(true);
        this.serialFactory = serialFactory;
        this.serialList = serialFactory.getSerialList();
        this.resultList = resultList;
//        this.setLayout(null);	//设置布局管理器
        setTitle("串口信息");// 窗体的标题
        this.setBounds(200,250,300,200);	//设置窗口的大小和位置
        this.add(getJPanel());
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                serialFactory.closeAllSerial();
//                dispose();
            }
        });
//        this.add(determineButton);
        this.setVisible(true);
    }

    public JPanel getJPanel(){
        JPanel jPanel = new JPanel();
        for (int i = 0; i < resultList.size(); i++){
            JPanel line = new JPanel();
            JLabel serialNumber = new JLabel();
            JLabel parmName = new JLabel();
            JLabel result = new JLabel();
            System.out.println(serialList.get(i));
            serialNumber.setText(serialList.get(i).getSerialNumber());
            parmName.setText(serialList.get(i).getParm().getName());
            result.setText((String) resultList.get(i).get("msg"));
            line.add(serialNumber);
            line.add(parmName);
            line.add(result);
            jPanel.add(line);
        }
        jPanel.add(getFooter());
        return jPanel;
    }

    public JPanel getFooter(){
        JPanel footer = new JPanel();
        footer.setPreferredSize(new Dimension(250, 50));
        JButton determineButton = new JButton("确定");
        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                serialFactory.closeAllSerial();
                dispose();
            }
        });
        determineButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                serialFactory.initWebSocket();
                dispose();
            }
        });
        footer.add(determineButton);
        footer.add(cancelButton);
        return footer;
    }
}


